import router from "@/router/index.js";
import {loginStore} from "@/store/modules/loginStore.js";
import {isLoginValid} from "@/utilities/authenticate.js";

// 白名单
const whiteList = ['/login']

// 全局路由守卫
router.beforeEach((to, from, next) => {
    // 获得登录的store
    const store = loginStore()

    // 判定当前的登录是否有效
    if (!isLoginValid() && store.loginTime!==0) {
        store.logout()
        return Promise.reject(new Error('token is timeout'))
    }

    if (store.token) {
        if (to.path === '/login') {
            next('/')
        } else {
            next()
        }
    } else {
        if (whiteList.includes(to.path)) {
            next()
        } else {
            next('/login')
        }
    }
})